//
//  ToResiduePayViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/26.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
#import "ToResiduePayViewController.h"
#import "RDVTabBarController.h"
#import "AttributedLabel.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "ServiceListViewController.h"
#import "OrderListViewController.h"
#import "QRCodeGenerator.h"
#import "UIButton+WebCache.h"
@interface ToResiduePayViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@end
@implementation ToResiduePayViewController
{
    UILabel*methodOfPayment;
    UIButton*cashBtn;
    UIButton*creditCardBtn;
    UIButton*payTreasureBtn;
    UIButton*weChatBtn;
    UIScrollView*myscrollView;
    UIButton *tureBtn;
    NSString *payType;
    NSString *payTypeName;
    NSString*strs;
    AttributedLabel*xuanze;
    UIButton *time;
    UIView*ditu;
    UIImageView*chaimageview;
    UILabel*payAmountLabel;
    UILabel *orderNo;
    UIButton* backButton;
    UILabel*titleStr;
    NSString* Token;
}
@synthesize alt = alt;
@synthesize delegate;
-(void)BackAction:(UIButton *)sender{
    [time removeFromSuperview];
    if ([self.sat isEqualToString:@"PlaceOrder"]) {
        OrderListViewController*de = [[OrderListViewController alloc] init];
        [self.navigationController pushViewController:de animated:YES];
        }
    if ([self.sat isEqualToString:@"fillPlaceOrder"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    if ([self.sat isEqualToString:@"ServiceOrder"]) {
        ServiceListViewController*de = [[ServiceListViewController alloc] init];
        [self.navigationController pushViewController:de animated:YES];
    }
    if ([self.sat isEqualToString:@"fillService"]) {
        ServiceListViewController*de = [[ServiceListViewController alloc] init];
        [self.navigationController pushViewController:de animated:YES];
    }
    if ([self.sat isEqualToString:@"Card"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    Token = [user objectForKey:@"tokenId"];
    self.navigationController.navigationBarHidden=YES;
    [[self rdv_tabBarController] setTabBarHidden:YES];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 0.3)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:37/255.0 green:60/255.0 blue:1/255.0 alpha:0.3]];
    lineLabel.alpha=0.3;
    [self.navigationController.navigationBar addSubview:lineLabel];
    UIView*_narview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    _narview.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
    [self.view addSubview:_narview];
    [self loadingtitle];
    self.title=@"收银";
    self.view.backgroundColor=[UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
}
-(void)loadingtitle{
    [backButton removeFromSuperview];
    [titleStr removeFromSuperview];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(10,27.5,30,30);
    backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    titleStr=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, 64)];
    titleStr.text=@"收银";
    titleStr.textColor=[UIColor whiteColor];
    titleStr.font=[UIFont systemFontOfSize:18];
    titleStr.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:titleStr];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
   if ([self.sat isEqualToString:@"PlaceOrder"]) {
        [self PlaceOrder];
    }
    if ([self.sat isEqualToString:@"fillPlaceOrder"]) {
        [self fillPlaceOrder];
    }
    if ([self.sat isEqualToString:@"ServiceOrder"]) {
        [self ServiceOrder];
    }
    
    if ([self.sat isEqualToString:@"fillService"]) {
        [self fillService];
    }
    
    if ([self.sat isEqualToString:@"card"]) {
        [self card];
    }
}
//储值卡收银
-(void)card{
    orderNo=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,40)];
    orderNo.text=[NSString stringWithFormat:@"    订单号 %@ %@",[self.Dict objectForKey:@"orderNo"],@"充值金额"];
    orderNo.textColor=luseColor;
    orderNo.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
    orderNo.font=[UIFont boldSystemFontOfSize:14];
    [self.view addSubview:orderNo];
    UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,0.35)];
    line.backgroundColor=luseColor;
    [self.view addSubview:line];
    self.orderId=[self.Dict objectForKey:@"orderId"];
    self.orderType=[NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"orderType"]];
    self.payAmount=[NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"payAmount"]];
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"payAmount"]];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
    [self loading:formattedNumberString];
    
}
//服务收银
-(void)ServiceOrder{
    
    orderNo=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,40)];
    orderNo.text=[NSString stringWithFormat:@"    订单号 %@ %@", [self.Dict objectForKey:@"orderNo"],@"服务金额"];
    orderNo.textColor=luseColor;
    orderNo.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
    orderNo.font=[UIFont boldSystemFontOfSize:14];
    [self.view addSubview:orderNo];
    UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,0.35)];
    line.backgroundColor=luseColor;
    [self.view addSubview:line];
    self. orderId=self. orderId;
    self.orderType=@"service";
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",self.payAmount];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
    [self loading:formattedNumberString];
    
}
//商品直接下订单收银
-(void)PlaceOrder{
    
    self.orderId=[self.Dict objectForKey:@"orderId"] ;
    self.orderType=@"order";
    orderNo=[[UILabel alloc]initWithFrame:CGRectMake(0,64, ScreenWidth,40)];
    orderNo.textColor=luseColor;
    orderNo.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
    orderNo.font=[UIFont boldSystemFontOfSize:14];
    [self.view addSubview:orderNo];
    UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,0.35)];
    line.backgroundColor=luseColor;
    [self.view addSubview:line];
    
    if ([[self.Dict objectForKey:@"gatheringType"]isEqualToString:@"E"]) {
        NSString *sat=@"收定金";
        orderNo.text=[NSString stringWithFormat:@"    订单号 %@ %@", [self.Dict objectForKey:@"orderNo"],sat];
        
    }
    if ([[self.Dict objectForKey:@"gatheringType"]isEqualToString:@"F"]) {
        NSString *sat=@"收全款";
        orderNo.text=[NSString stringWithFormat:@"    订单号 %@ %@", [self.Dict objectForKey:@"orderNo"],sat];
        
    }
    if ([[self.Dict objectForKey:@"gatheringType"]isEqualToString:@"R"]) {
        NSString *sat=@"收尾款";
        orderNo.text=[NSString stringWithFormat:@"    订单号 %@ %@", [self.Dict objectForKey:@"orderNo"],sat];
        
    }
    
    self.payAmount=[NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"payAmount"]];
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"payAmount"]];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
    [self loading:formattedNumberString];
    
}
- (void)cashBtn:(UIButton *)button{
    methodOfPayment.textColor=[UIColor clearColor];
    payType=@"C";
    [cashBtn setTitleColor:shenluse forState:UIControlStateNormal];
    [creditCardBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [payTreasureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [weChatBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self cashCashier];
}
- (void)creditCardBtn:(UIButton *)button{
    methodOfPayment.textColor=[UIColor clearColor];
    payType=@"B";
    [cashBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [creditCardBtn setTitleColor:shenluse forState:UIControlStateNormal];
    [payTreasureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [weChatBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self cashCashier];
    }
- (void)payTreasure:(UIButton *)button{
    [chaimageview removeFromSuperview];
    [myscrollView removeFromSuperview];
    methodOfPayment.textColor=[UIColor clearColor];
    if (button.tag==0) {
        payType=@"AliQrPay";
        payTypeName=@"支付宝支付";
        [payTreasureBtn setTitleColor:shenluse forState:UIControlStateNormal];
        [weChatBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    if (button.tag==1) {
        payType=@"WxScanPay";
        payTypeName=@"微信支付";
        [payTreasureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [weChatBtn setTitleColor:shenluse forState:UIControlStateNormal];
    }
    [cashBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [creditCardBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    ditu=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight+40)];
    ditu.backgroundColor=backColor;
    ditu.alpha=0.6;
    [self.view addSubview:ditu];
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [ditu addGestureRecognizer:singleFingerOne];
    
    myscrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,payAmountLabel.bottom, ScreenWidth, ScreenWidth)];
    [myscrollView setBackgroundColor: [UIColor whiteColor]];
    myscrollView.delegate = self;
    myscrollView.userInteractionEnabled = YES;
    myscrollView.scrollEnabled = YES;
    myscrollView.showsVerticalScrollIndicator = FALSE;
    myscrollView.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:myscrollView];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    [params setValue:self.orderType forKey:@"orderType"];
    [params setValue:self.orderId  forKey:@"orderId"];
    [params setValue:[NSString stringWithFormat:@"%.2f",  [self.payAmount doubleValue]] forKey:@"payAmount"];
    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:[NSString stringWithFormat:@"Business/Pay/%@",payType] params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                self.gid=[severDataDict.dataDict objectForKey:@"gid"];
                [self times];
                [self str:payTypeName priceStr:payAmountLabel.text QRStr:[severDataDict.dataDict  objectForKey:@"qrCode"]];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    chaimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-50, payAmountLabel.bottom-15, 30, 30)];
    chaimageview .image=[UIImage imageNamed:@"chas"];
    [self.view addSubview:chaimageview];
}

//生成二维码
-(void)str:(NSString*)str priceStr:(NSString*)priceStr QRStr:(NSString*)QRStr{
    
    NSString *s12 = [NSString stringWithFormat:@"%@",  @"你选择"];
    UIFont *font12 = [UIFont systemFontOfSize:15];
    CGSize size12 = CGSizeMake(320, 2000);
    CGSize labelsize12 = [s12 sizeWithFont:font12 constrainedToSize:size12 lineBreakMode:NSLineBreakByCharWrapping];
    UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2-100, 27, labelsize12.width, 15)];
    label1.text=[NSString stringWithFormat:@"%@",@"你选择"];
    label1.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    label1.font=[UIFont systemFontOfSize:15];
    [myscrollView addSubview:label1];
    
    NSString *s1 = [NSString stringWithFormat:@"%@",  str];
    UIFont *font1= [UIFont systemFontOfSize:15];
    CGSize size1= CGSizeMake(320, 2000);
    CGSize labelsize1= [s1 sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];
    UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(label1.right+5, 27, labelsize1.width+10, 15)];
    label2.text=[NSString stringWithFormat:@"%@",str];
    label2.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    label2.textColor=[UIColor whiteColor];
    label2.textAlignment=NSTextAlignmentCenter;
    label2.font=[UIFont systemFontOfSize:14];
    [myscrollView addSubview:label2];
    
    UILabel*label3=[[UILabel alloc]initWithFrame:CGRectMake(label2.right+5, 27, 100, 15)];
    label3.text=[NSString stringWithFormat:@"%@",@"需付金额:"];
    label3.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    label3.font=[UIFont systemFontOfSize:15];
    [myscrollView addSubview:label3];
    
    UILabel*payAmounts=[[UILabel alloc]initWithFrame:CGRectMake(0, label1.bottom+7.5, ScreenWidth, 24)];
    payAmounts.text=[NSString stringWithFormat:@"%@",payAmountLabel.text];
    payAmounts.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    payAmounts.textAlignment=NSTextAlignmentCenter;
    payAmounts.font=[UIFont systemFontOfSize:24];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",payAmountLabel.text]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:14]
     
                          range:NSMakeRange(0, 1)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]
     
                          range:NSMakeRange(0, 1)];
    payAmounts.attributedText = AttributedStr;
    [myscrollView addSubview:payAmounts];
    tureBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2-75, payAmounts.bottom+132/2, 150, 150)];
    [tureBtn setImageWithURL:[NSURL URLWithString:QRStr]];
    tureBtn.contentMode = UIViewContentModeScaleAspectFit;
    [myscrollView addSubview:tureBtn];
    [myscrollView setContentSize:CGSizeMake(ScreenWidth, tureBtn.bottom+60)];
    
}
//现金支付//刷卡支付
-(void)cashCashier{
    [myscrollView removeFromSuperview];
    [chaimageview removeFromSuperview];
    ditu=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight+40)];
    ditu.backgroundColor=backColor;
    ditu.alpha=0.6;
    [self.view addSubview:ditu];
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [ditu addGestureRecognizer:singleFingerOne];
    myscrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,payAmountLabel.bottom-0.5, ScreenWidth, ScreenWidth)];
    [myscrollView setBackgroundColor: [UIColor whiteColor]];
    myscrollView.delegate = self;
    myscrollView.userInteractionEnabled = YES;
    myscrollView.scrollEnabled = YES;
    myscrollView.showsVerticalScrollIndicator = FALSE;
    myscrollView.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:myscrollView];
    
    if ([payType isEqualToString:@"C"]) {
        strs=@"现金支付";
    }
    if ([payType isEqualToString:@"B"]) {
        strs=@"刷卡支付";
    }
        NSString *s12 = [NSString stringWithFormat:@"%@",  @"你选择"];
        UIFont *font12 = [UIFont systemFontOfSize:15];
        CGSize size12 = CGSizeMake(320, 2000);
        CGSize labelsize12 = [s12 sizeWithFont:font12 constrainedToSize:size12 lineBreakMode:NSLineBreakByCharWrapping];
        UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2-100, height5or6, labelsize12.width, 15)];
        label1.text=[NSString stringWithFormat:@"%@",@"你选择"];
        label1.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        label1.font=[UIFont systemFontOfSize:15];
        [myscrollView addSubview:label1];
        
        NSString *s1 = [NSString stringWithFormat:@"%@",  strs];
        UIFont *font1= [UIFont systemFontOfSize:15];
        CGSize size1= CGSizeMake(320, 2000);
        CGSize labelsize1= [s1 sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];
        UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(label1.right+5, height5or6, labelsize1.width+10, 15)];
        label2.text=[NSString stringWithFormat:@"%@",strs];
        label2.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
        label2.textColor=[UIColor whiteColor];
        label2.textAlignment=NSTextAlignmentCenter;
        label2.font=[UIFont systemFontOfSize:14];
        [myscrollView addSubview:label2];
        UILabel*label3=[[UILabel alloc]initWithFrame:CGRectMake(label2.right+5, height5or6, 100, 15)];
        label3.text=[NSString stringWithFormat:@"%@",@"需付金额:"];
        label3.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        label3.font=[UIFont systemFontOfSize:15];
        [myscrollView addSubview:label3];
        UILabel*payAmounts=[[UILabel alloc]initWithFrame:CGRectMake(0, label1.bottom+7.5, ScreenWidth, 24)];
        payAmounts.text=[NSString stringWithFormat:@"%@",payAmountLabel.text];
        payAmounts.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        payAmounts.textAlignment=NSTextAlignmentCenter;
        payAmounts.font=[UIFont systemFontOfSize:24];
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",payAmountLabel.text]];
        
        [AttributedStr addAttribute:NSFontAttributeName
         
                              value:[UIFont systemFontOfSize:14]
         
                              range:NSMakeRange(0, 1)];
        
        [AttributedStr addAttribute:NSForegroundColorAttributeName
         
                              value:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]
         
                              range:NSMakeRange(0, 1)];
        payAmounts.attributedText = AttributedStr;
        
        [myscrollView addSubview:payAmounts];
        tureBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2-50, payAmounts.bottom+80, 215/2,  215/2)];
        tureBtn.layer.masksToBounds = YES;
        tureBtn.backgroundColor= luseColor;
        tureBtn.layer.cornerRadius =  215/2/2;
        [tureBtn setTitle:@"确认" forState:UIControlStateNormal];
        [tureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [tureBtn addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
        tureBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        tureBtn.layer.borderWidth = 0;
        tureBtn.contentMode = UIViewContentModeScaleAspectFit;
        tureBtn.titleLabel.font=[UIFont systemFontOfSize:18];
        [myscrollView addSubview:tureBtn];
        [myscrollView setContentSize:CGSizeMake(ScreenWidth, tureBtn.bottom+60)];
    chaimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-50, payAmountLabel.bottom-15, 30, 30)];
    chaimageview .image=[UIImage imageNamed:@"chas"];
    [self.view addSubview:chaimageview];
}
//直接收银//服务收银
- (void)ture:(UIButton *)button{
if ([payType length] <1)
        {
            [self showAlertMessage:@"请选择支付方式"];
        }
        else{
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:Token forKey:@"tokenId"];
            [params setValue:[self.Dict objectForKey:@"orderId"] forKey:@"orderId"];
            [params setValue:self.orderType forKey:@"orderType"];
            [params setValue:[NSString stringWithFormat:@"%.2f",  [[self.Dict objectForKey:@"payAmount"]doubleValue]] forKey:@"payAmount"];
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
            [HttpRequest requestData:@"Business/Pay/CashPay" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        [self backView];
                        }else{
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                    }
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }}];
            
        }
        [time removeFromSuperview];
    
    
}
- (void)times{
    
    time = [[UIButton alloc]init];
    time.frame = CGRectMake(ScreenWidth-100,ScreenHeight*35/2200,30,16);
    [time setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    [time setTitle:[NSString stringWithFormat:@"%d",10] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [myscrollView addSubview:time];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownActions:) userInfo:time repeats:YES];
}
//自动更新二维码是否收银成功
-(void)countDownActions:(NSTimer*)timer{
    time = timer.userInfo;
    if (!time.superview) {
        [timer invalidate];
        return;
    }
    int currentTime = [[time titleForState:UIControlStateNormal] intValue];
    [time setTitle:[NSString stringWithFormat:@"%d",--currentTime] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont systemFontOfSize:16];
    if (currentTime<1) {
        
        [time setTitle:[NSString stringWithFormat:@"%d",10] forState:UIControlStateNormal];
        [self times];
        [time removeFromSuperview];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:self.gid forKey:@"gid"];
        [HttpRequest requestData:@"Business/Gathering/Detail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                NSLog(@"－－%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    if ([[severDataDict.dataDict  objectForKey:@"status"] isEqualToString:@"N"]) {
                        }if ( [[severDataDict.dataDict  objectForKey:@"status"] isEqualToString:@"Y"]) {
                        
                        [time removeFromSuperview];
                        [timer invalidate];
                        [self backView];
                        }
                    }else{
                    }
                }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [timer invalidate];
                [Global showTextHudSub:@"连接失败" view:self.view];}
            }];
    }
}
-(void)loading: (NSString*)formattedNumberString{
    payAmountLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, orderNo.bottom, ScreenWidth-15,ScreenHeight-ScreenWidth-60-100)];
    payAmountLabel.text=[NSString stringWithFormat:@"¥ %@", formattedNumberString];
    payAmountLabel.textAlignment = NSTextAlignmentRight;
    payAmountLabel.font=[UIFont boldSystemFontOfSize:PriceFot];
    payAmountLabel.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
    payAmountLabel.textColor=[UIColor colorWithRed:225/255.0 green:238/255.0 blue:203/255.0 alpha:1.0];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@" ,formattedNumberString]];
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:30.0]
     
                          range:NSMakeRange(0, 1)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor colorWithRed:225/255.0 green:238/255.0 blue:203/255.0 alpha:1.0]
     
                          range:NSMakeRange(0, 1)];
    payAmountLabel.attributedText = AttributedStr;
    [self.view addSubview:payAmountLabel];
    UILabel*bianView=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-15, orderNo.bottom,15 ,ScreenHeight-ScreenWidth-60-100)];
    bianView.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
    [self.view addSubview:bianView];
    methodOfPayment=[[UILabel alloc]initWithFrame:CGRectMake(0, payAmountLabel.bottom, ScreenWidth, 355)];
    methodOfPayment.text=@"请选择支付方式";
    methodOfPayment.textColor=zhonghuiColor;
    methodOfPayment.font=[UIFont systemFontOfSize:16];
    methodOfPayment.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:methodOfPayment];
    
    UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-60, ScreenWidth/4, 60)];
    btn.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    [btn addTarget:self action:@selector(cashBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    cashBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-20, ScreenWidth/4, 13)];
    [cashBtn  setTitle:@"现金" forState:UIControlStateNormal];
    [cashBtn addTarget:self action:@selector(cashBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cashBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    cashBtn.titleLabel .font=[UIFont systemFontOfSize:13];
    [self.view addSubview:cashBtn];
    UIButton*imagebtn=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-60, ScreenWidth/4, 40)];
    [imagebtn addTarget:self action:@selector(cashBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:imagebtn];
    UIImageView*imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)];
    imageview.image=[UIImage imageNamed:@"xianjins"];
    imageview.center=imagebtn.center;
    [self.view addSubview:imageview];
    
    UIButton*shuakabtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4, ScreenHeight-60, ScreenWidth/4, 60)];
    shuakabtn.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    [shuakabtn addTarget:self action:@selector(creditCardBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shuakabtn];
    creditCardBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4, ScreenHeight-20, ScreenWidth/4, 13)];
    [creditCardBtn  setTitle:@"刷卡" forState:UIControlStateNormal];
    [creditCardBtn addTarget:self action:@selector(creditCardBtn:) forControlEvents:UIControlEventTouchUpInside];
    [creditCardBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    creditCardBtn.titleLabel .font=[UIFont systemFontOfSize:13];
    [self.view addSubview:creditCardBtn];
    UIButton*creditCardBtnimagebtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4, ScreenHeight-60, ScreenWidth/4, 40)];
    [creditCardBtnimagebtn addTarget:self action:@selector(creditCardBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:creditCardBtnimagebtn];
    UIImageView*creditCardBtnimagebtnimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/4, 0, 45, 45)];
    creditCardBtnimagebtnimageview.image=[UIImage imageNamed:@"shuakas"];
    creditCardBtnimagebtnimageview.center=creditCardBtnimagebtn.center;
    [self.view addSubview:creditCardBtnimagebtnimageview];
    
    UIButton*payTreasureBtn1=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*2, ScreenHeight-60, ScreenWidth/4, 60)];
    payTreasureBtn1.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    payTreasureBtn1.tag=0;
    [payTreasureBtn1 addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:payTreasureBtn1];
    payTreasureBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*2, ScreenHeight-20, ScreenWidth/4, 13)];
    [payTreasureBtn  setTitle:@"支付宝" forState:UIControlStateNormal];
    payTreasureBtn.tag=0;
    [payTreasureBtn addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
    [payTreasureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    payTreasureBtn.titleLabel .font=[UIFont systemFontOfSize:13];
    [self.view addSubview:payTreasureBtn];
    UIButton*zhifubaobtn1imagebtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*2, ScreenHeight-60, ScreenWidth/4, 40)];
    [zhifubaobtn1imagebtn addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
     zhifubaobtn1imagebtn.tag=0;
    [self.view addSubview:zhifubaobtn1imagebtn];
    UIImageView*zhifubaobtnimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/4*2, 0, 45, 45)];
    zhifubaobtnimageview.image=[UIImage imageNamed:@"zhifubaos"];
    zhifubaobtnimageview.center=zhifubaobtn1imagebtn.center;
    [self.view addSubview:zhifubaobtnimageview];
    
    UIButton*weChatBtn1=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*3, ScreenHeight-60, ScreenWidth/4, 60)];
    weChatBtn1.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    weChatBtn1.tag=1;
    [weChatBtn1 addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:weChatBtn1];
    weChatBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*3, ScreenHeight-20, ScreenWidth/4, 13)];
    [weChatBtn  setTitle:@"微信" forState:UIControlStateNormal];
    weChatBtn.tag=1;
    [weChatBtn addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
    [weChatBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    weChatBtn.titleLabel .font=[UIFont systemFontOfSize:13];
    [self.view addSubview:weChatBtn];
    UIButton*weChatBtnimagebtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*3, ScreenHeight-60, ScreenWidth/4, 40)];
    [weChatBtnimagebtn addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
    weChatBtnimagebtn.tag=1;
    [self.view addSubview:weChatBtnimagebtn];
    UIImageView*weChatBtnimagebtnimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/4*3, 0, 45, 45)];
    weChatBtnimagebtnimageview.image=[UIImage imageNamed:@"weixins"];
    weChatBtnimagebtnimageview.center=weChatBtnimagebtn.center;
    [self.view addSubview:weChatBtnimagebtnimageview];
  
}
-(void)backView{
    if ([self.sat isEqualToString:@"PlaceOrder"]) {
        OrderListViewController*de = [[OrderListViewController alloc] init];
        [self.navigationController pushViewController:de animated:YES];
    }
    if ([self.sat isEqualToString:@"ServiceOrder"]) {
        ServiceListViewController*de = [[ServiceListViewController alloc] init];
        [self.navigationController pushViewController:de animated:YES];
    }
    if ([self.sat isEqualToString:@"fillService"]) {
        [self.navigationController popViewControllerAnimated:YES];
        }
    if ([self.sat isEqualToString:@"fillPlaceOrder"]) {
        OrderListViewController*de = [[OrderListViewController alloc] init];
        [self.navigationController pushViewController:de animated:YES];
    }
    if ([self.sat isEqualToString:@"card"]) {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
    }
}
-(void)closeKeyBoad
{   methodOfPayment.textColor=zhonghuiColor;
    [myscrollView removeFromSuperview];
    [ditu removeFromSuperview];
    [chaimageview removeFromSuperview];
    [time removeFromSuperview];
}

//服务补收获取订单数据
-(void)fillService{
    self.orderType=@"service";
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    [params setValue:self.orderId forKey:@"serviceId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/Service/ToPay" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"33333333====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                self.Dict=severDataDict.dataDict;
                self.orderId=[NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"serviceId"]];
                orderNo=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,40)];
                orderNo.textColor=luseColor;
                orderNo.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
                orderNo.font=[UIFont boldSystemFontOfSize:14];
                [self.view addSubview:orderNo];
                UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,0.35)];
                line.backgroundColor=luseColor;
                [self.view addSubview:line];
                orderNo.text=[NSString stringWithFormat:@"    订单号 %@ %@", [self.Dict objectForKey:@"serviceNo"],@"服务金额"];
                self.payAmount=[NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"payAmount"]];
                NSString*GoldPrice=[NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"payAmount"]];
                NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                [numberFormatter setPositiveFormat:@"###,##0.00;"];
                NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
                [self loading:formattedNumberString];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }}
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
    }];
}
//订单补收获取订单数据
-(void)fillPlaceOrder{
    self.orderType=@"order";
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    [params setValue:[self.Dict objectForKey:@"orderId"] forKey:@"orderId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/Order/ToResiduePay" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"33333333====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                self.Dict=severDataDict.dataDict;
                orderNo=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,40)];
                orderNo.textColor=luseColor;
                orderNo.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
                orderNo.font=[UIFont boldSystemFontOfSize:14];
                [self.view addSubview:orderNo];
                UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,0.35)];
                line.backgroundColor=luseColor;
                [self.view addSubview:line];
                if ([[self.Dict objectForKey:@"gatheringType"]isEqualToString:@"E"]) {
                    NSString *sat=@"收定金";
                    orderNo.text=[NSString stringWithFormat:@"    订单号 %@ %@", [self.Dict objectForKey:@"orderNo"],sat];}
                if ([[self.Dict objectForKey:@"gatheringType"]isEqualToString:@"F"]) {
                    NSString *sat=@"收全款";
                    orderNo.text=[NSString stringWithFormat:@"    订单号 %@ %@", [self.Dict objectForKey:@"orderNo"],sat];}
                if ([[self.Dict objectForKey:@"gatheringType"]isEqualToString:@"R"]) {
                    NSString *sat=@"收尾款";
                    orderNo.text=[NSString stringWithFormat:@"    订单号 %@ 已收定金¥ %@ %@", [self.Dict objectForKey:@"orderNo"], [self.Dict objectForKey:@"earnestMoney"],sat];
                    orderNo.font=[UIFont boldSystemFontOfSize:14];
                   }
                NSString*payAmount=[NSString stringWithFormat:@"%@",  [NSString stringWithFormat:@"%@",[self.Dict objectForKey:@"payAmount"]]];
                self.payAmount=payAmount;
                NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                [numberFormatter setPositiveFormat:@"###,##0.00;"];
                NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:self.payAmount.floatValue]];
                [self loading:formattedNumberString];
               }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }}
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}
- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end
